var checkedlist = {
    page: 1,
    init: function() {
        var that = this;
        that.dateInit();


        that.doData();

        // 查询
        $("#J_query").click(function() {
            // 重置分页
            that.page = 1;
            that.doData();
        });
        //close
        $("body").on('click', '#noticeClose,#noticeCancel', function() {
            $("#issueTmp").remove();
        });
        that.initPublish();
    },
    /**
     * 发布公告
     */
    initPublish: function(){
        $('#J_publish').on('click', function(){
            // 清除数据
            $('#publishModel').modal();
        });
    },
    dateInit: function() {
        $('.datepicker').datetimepicker({
            language: 'zh-CN',
            weekStart: 1,
            todayBtn: 1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            minView: 2,
            forceParse: 0,
            format: 'yyyy-mm-dd hh:ii'
        });
    },
    /**
     * 页面数据渲染
     * @return {[type]} [description]
     */
    doData: function() {
        var that = this;
        var release_time1 = $('#release_time1').val();
        var release_time2 = $('#release_time2').val(); 
        $.ajax({
            url: COMMONURL + "api/Notice/GetPage?token=" + $.cookie('token'),
            type: "post",
            data: {
                release_time1: release_time1,
                release_time2: release_time2,
                page: that.page,
                pagesize: 20
            },
            dataType: "json"
        }).done(function(redata) {
            if (redata.recode == '0000') {
                var compiled = _.template($("#HistoryTpl").html());
                var odata = redata.mod;
                for (var i = 0, length = odata.length; i < length; i++) {
                    odata[i].start_bid_time = that.formatTime(odata[i].start_bid_time);
                    odata[i].end_bid_time = that.formatTime(odata[i].end_bid_time);
                }
                $("#history_con").html(compiled(odata.Items));
                $("#history-page").show().jqPaginator({
                    //totalPages: Math.ceil(redata.mod.TotalNum/20),
                    totalPages: redata.mod.TotalPageCount,
                    visiblePages: 5,
                    currentPage:  that.page,
                    first: '<li class="first"><a href="javascript:void(0);">首页<\/a><\/li>',
                    prev: '<li class="prev"><a href="javascript:void(0);"><i class="arrow arrow2"><\/i>上一页<\/a><\/li>',
                    next: '<li class="next"><a href="javascript:void(0);">下一页<i class="arrow arrow3"><\/i><\/a><\/li>',
                    last: '<li class="last"><a href="javascript:void(0);">末页<\/a><\/li>',
                    page: '<li class="page"><a href="javascript:void(0);">{{page}}<\/a><\/li>',
                    onPageChange: function(num, type) {

                        that.page = num;
                        if (type == 'change') {
                            that.doData();
                        }
                    }
                });

                $(".jLook").click(function() {

                    var $noticeIssue = $("#noticeIssue"),
                        $issueTpl = $("#issueTpl");
                    var winHeight = $(document).height();
                    var id = $(this).attr("data-id");
                    $.ajax({
                        url: COMMONURL + "api/Notice/Get?token=" + $.cookie('token'),
                        type: "get",
                        data: {
                            id: id

                        },
                        dataType: "json"
                    }).done(function(redata) {
                        if (redata.recode == '0000') {
                            var compiled = _.template($issueTpl.html());
                            $('body').append(compiled(redata.mod));
                            $("#issueTmp #issueMask").css('height', winHeight);
                        } else {
                            alert(data.resmessage);
                        }
                    });

                })
                



            }
        });
    },
    formatTime: function(nS) {
           if(!nS) return null;
            var date = new Date(nS*1000);
            var year = date.getFullYear();
            var month = date.getMonth()+1;
            var d = date.getDate();
            var hours = date.getHours();
            var minutes = date.getMinutes();
            var seconds = date.getSeconds();
            return year+'-'+month+'-'+d+' '+hours+':'+minutes;
    }
}

checkedlist.init();
